import type { PageParam, PageResult } from '@vben/request';
import type { Dayjs } from 'dayjs';

import { requestClient } from '#/api/request';

export namespace OrderApi {
  /** 婚礼订单主信息 */
  export interface Order {
    id: number;
    orderId?: string;
    customerId?: string;
    weddingDate?: string | Dayjs;
    totalAmount: number;
    discountAmount: number;
    finalAmount: number;
    paidAmount: number;
    packageId: string;
    orderStatus: string;
  }

  /** 服务项 */
  export interface ServiceItem {
    id?: number;
    itemId: string;
    serviceType: string;
    resourceCode: string;
    resourceName: string;
    price: number;
    quantity: number;
    subtotal: number;
    remarks?: string;
  }

  /** 订单时间轴项 */
  export interface OrderTimelineItem {
    id: number;
    orderId: string;
    action: string;
    description: string;
    operator: string;
    operateTime: string;
  }
}

// ==================== 标准CRUD接口 (/wedding/order) ====================

/** 查询婚礼订单主分页 */
export function getOrderPage(params: PageParam) {
  return requestClient.get<PageResult<OrderApi.Order>>('/wedding/order/page', { params });
}

/** 查询婚礼订单主详情 */
export function getOrder(id: number) {
  return requestClient.get<OrderApi.Order>(`/wedding/order/get?id=${id}`);
}

/** 新增婚礼订单主 */
export function createOrder(data: OrderApi.Order) {
  return requestClient.post('/wedding/order/create', data);
}

/** 修改婚礼订单主 */
export function updateOrder(data: OrderApi.Order) {
  return requestClient.put('/wedding/order/update', data);
}

/** 删除婚礼订单主 */
export function deleteOrder(id: number) {
  return requestClient.delete(`/wedding/order/delete?id=${id}`);
}

/** 导出婚礼订单主 */
export function exportOrder(params: any) {
  return requestClient.download('/wedding/order/export-excel', { params });
}

// ==================== 扩展功能接口 (/wedding/resource/order) ====================

const resourceBaseUrl = '/wedding/resource/order';

/** 获取订单时间轴 */
export function getOrderTimeline(orderId: string) {
  return requestClient.get<OrderApi.OrderTimelineItem[]>(
    `${resourceBaseUrl}/timeline`,
    { params: { orderId } },
  );
}

/** 获取订单服务项 */
export function getOrderServiceItems(orderId: string) {
  return requestClient.get<OrderApi.ServiceItem[]>(
    `${resourceBaseUrl}/service-items`,
    { params: { orderId } },
  );
}

/** 获取支付记录 */
export function getPaymentRecords(orderId: string) {
  return requestClient.get<Array<{
    id: number;
    orderId: string;
    paymentAmount: number;
    paymentMethod: string;
    paymentTime: string;
    transactionNo: string;
  }>>(`${resourceBaseUrl}/payment-records`, {
    params: { orderId },
  });
}

/** 确认订单 */
export function confirmOrder(orderId: string) {
  return requestClient.post(`${resourceBaseUrl}/action/confirm`, { orderId });
}

/** 取消订单 */
export function cancelOrder(orderId: string, reason: string) {
  return requestClient.post(`${resourceBaseUrl}/action/cancel`, {
    orderId,
    reason,
  });
}


